package com.ruoyi.vplat.websocket.listener.acs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.websocket.Session;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.websocket.handler.AbstractWebsocketMsgHandler;
import com.ruoyi.common.websocket.server.WebSocketUtils;
import com.ruoyi.vplat.util.VplatConstant;
import com.ruoyi.vplat.websocket.listener.AbstractWsOpenListener;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class ACSWsOpenListener extends AbstractWsOpenListener {
	@Override
	public void onOpen(String username, Session session) throws Exception {
		String gateway = username.toString();
		String protocol = "6";
		String gatewayId = saveOrUpdateGateway(username, session, "私有服务", protocol);
		sendDevicesToGateway(gatewayId, gateway, protocol);

	}

	/**
	 * @param gatewayId
	 * @param gateway
	 * @param protocol
	 * @throws Exception
	 */
	public void sendDevicesToGateway(String gatewayId, String gateway, String protocol) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("gatewayId", gatewayId);
		List<Map<String, Object>> devices = vplatDeviceService.queryList(params);
		JSONArray arr = new JSONArray();
		for (int i = 0; i < devices.size(); i++) {
			Map<String, Object> device = devices.get(i);
			JSONObject item = getDeviceInfo(device);
			arr.add(item);
			if (arr.size() >= 30) {
				sendDevicesToGateway(gateway, protocol, arr);
				arr.clear();
			}
		}
		if (arr.size() > 0) {
			sendDevicesToGateway(gateway, protocol, arr);
		}
	}

	/**
	 * @param gateway
	 * @param protocol
	 * @param arr
	 */
	public void sendDevicesToGateway(String gateway, String protocol, JSONArray arr) {
		Integer seqNum = AbstractWebsocketMsgHandler.getRandomNum(7);
		int num = Integer.parseInt(seqNum.toString());
		JSONObject commandTemplate = AbstractWebsocketMsgHandler.getCommandTemplate("25", new HashMap<String, Object>() {
			{
				put("protocol", Integer.parseInt(protocol));
				put("seqNum", num);
				put("clientId", VplatConstant.System.NAME);
				put("devList", arr);
				put("operate", "add");
				put("gatewayId", gateway);
			}
		});
		WebSocketUtils.sendMessage(gateway, commandTemplate);
		log.info("下发设备至\n{}\n{}", gateway, commandTemplate);

	}

	/**
	 * @param device
	 * @return
	 */
	public JSONObject getDeviceInfo(Map<String, Object> device) {
		JSONObject item = new JSONObject();
		item.put("szDevId", device.get("id"));
		item.put("szDevName", device.get("name"));
		item.put("szIp", device.get("ip"));
		String port = (String) device.get("port");
		if (StringUtils.isBlank(port)) {
			item.put("iPort", 8000);
		} else {
			item.put("iPort", Integer.parseInt(port));
		}
		String factory = (String) device.get("factory");
		if (StringUtils.isBlank(factory)) {
			item.put("eManufature", 0);
		} else {
			item.put("eManufature", Integer.parseInt(factory));
		}
		item.put("szUserName", device.get("username"));
        item.put("szUserPwd", device.get("password"));

		
		return item;
	}

}
